// view/index/bossquan/bossquan.js
let app = getApp()
let yuming = app.globalData.yuming
const request = require('../../config/request');
var shuzong = []
var shu = []
Page({
  /**
   * 页面的初始数据
   */
  ...request,
  data: {
    gongwu: false,
    // zanwutu: false,
    yuming: yuming,
    heights: "",
    title: '区域广场',

    currtab: 0,
    photo: yuming + 'static/applet/images/my/mybj.png',
    xiao: [],
    category: 1, //1广场，2已关注的，3我的
    avatar: '',
    nickname: '',
    // shopBosscircle  动态 shopfollow  关注   shopCoverfollow  粉丝
    bossBackg: '', //上传之后的背景图
    dian: false, //是否点赞
    shopId: wx.getStorageSync('shopId'),
    page: 1,
    imageSrc: '',
    tuisshow: false,
  },
  // 删除员工
  delete(e) {
    var id = e.currentTarget.dataset.id
    var that = this
    wx.showModal({
      title: '您确定要删除该动态吗？',
      cancelText: '否',
      confirmText: '是',
      success(res) {
        if (res.confirm) {
          console.log("是")
          that.shan(id)
        } else {
          console.log("否")
        }
      }
    })
  },
  shan(id) {
    var that = this
    wx.request({
      url: yuming + 'shop/shopDelBossCircle',
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        token: wx.getStorageSync('token'),
        source: 'EabEnd'
      },
      data: {
        id: id,
        shopId: wx.getStorageSync('shopId')
      },
      success: function (res) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        if (res.data.code == 200) {
          that.liebiao()
        }
      },
      fail: function () {
        app.consoleLog("请求数据失败");
      }
    })
  },
  // 点赞
  dianzan(e) {
    var id = e.currentTarget.dataset.id
    var that = this
    wx.request({
      url: yuming + 'shop/shopPubBossCircle',
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        token: wx.getStorageSync('token'),
        source: 'EabEnd'
      },
      data: {
        shopId: wx.getStorageSync('shopId'),
        id: id
      },
      success: function (res) {
        if (res.data.code == 200) {
          that.liebiao()
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      },
      fail: function () {
        app.consoleLog("请求数据失败");
      }
    })
  },
  // 关注
  yiguan() {
    var that = this
    wx.request({
      url: yuming + 'shop/shopAddBossfollow',
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        token: wx.getStorageSync('token'),
        source: 'EabEnd'
      },
      data: {
        shopId: wx.getStorageSync('shopId'),
        fid: that.data.shop.id
      },
      success: function (res) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        that.liebiao()
      },
      fail: function () {
        app.consoleLog("请求数据失败");
      }
    })
  },
  // 查看更多
  lookmore() {
    shuzong = this.data.xiao
    this.setData({
      page: (this.data.page) + 1
    })
    this.liebiao()
  },
  // 列表
  liebiao() {
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    // that.setData({
    //   zanwutu: false
    // })
    this.myRequest(yuming + 'shop/shopSelectBossCircle', {
      page: that.data.page,
      shopId: wx.getStorageSync('shopId'),
      category: that.data.category,
      sonId: wx.getStorageSync('subId'), //子账号id
    }, 'POST').then(res => { // wx.hideLoading({})
      wx.hideLoading();
      if (res.data.code == 200) {
        shu = res.data.data
        shu.forEach(item => {
          if (item.shopLogo == null) {
            item.shopLogo = '../../images/mrtx.png'
          }
        })
        if (that.data.page != 1) {
          if (shu.length != 10) {
            wx.showToast({
              title: '没有更多数据了~',
              icon: "none"
            })
            that.setData({
              // xiao: [],
              gongwu: true,
              // zanwutu: true
            })
          }
        }
        that.setData({
          xiao: shuzong.concat(shu),
          shop: res.data.shop,
          shopCoverfollow: res.data.shopCoverfollow,
          bossBackg: res.data.shop.bossBackg,
          nickname: res.data.user.nickname,
          avatar: res.data.shop.shopLogo,
          huan: [{
            name: '广场',
            shu: res.data.shopBosscircle,
            id: '1'
          }, {
            name: '关注',
            shu: res.data.shopfollow,
            id: '2'
          }, {
            name: '我的',
            shu: res.data.shopCoverfollow,
            id: '3'
          }],
          // shopBosscircle  动态 shopfollow  关注   shopCoverfollow  粉丝
        })
        if (this.data.shop.shopLogo == null) {
          this.data.shop.shopLogo = '../../images/mrtx.png'
        }
        this.setData({
          shop: this.data.shop
        })
      }
    })
  },
  // 放大
  fangda(e) {
    var img = e.currentTarget.dataset.img
    var list = e.currentTarget.dataset.list
    var arr = []
    for (let i in list) {
      arr.push(list[i]); //属性
    }
    console.log(list)

    console.log(arr)

    wx.previewImage({
      current: img,
      urls: arr
    })
  },
  // boss详情
  bossxq(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/page_two/bossxq/bossxq?id=' + id,
    })
  },
  chuan(e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: yuming + 'Besall/Upload',
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {

          },
          success(res) {
            var data = JSON.parse(res.data)
            wx.showToast({
              title: data.msg,
              icon: 'none'
            })
            var urls = data.httpUrl + data.imgUrl
            wx.navigateTo({
              url: '/page_two/upload/upload?src=' + urls,
            })
          }
        })
      }
    })
  },
  // 发布动态
  fabu() {
    wx.navigateTo({
      url: '/page_two/fadt/fadt?avatar=' + this.data.avatar + "&nickname=" + this.data.nickname,
    })
  },
  // tab切换
  qies(e) {
    var index = e.currentTarget.dataset.currtab
    var id = e.currentTarget.dataset.id
    this.setData({
      currtab: index,
      category: id,
      page: 1,
      xiao: []
    })
    shuzong = []
    shu = []
    this.liebiao()
  },
  backs() {
    wx.reLaunch({
      url: '/pages/index/index',
    })
    // wx.navigateBack({
    //   delta: 0
    // })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      // title: options.title,
      shopId: wx.getStorageSync('shopId')
    })
    // this.setData({
    //   category: this.data.huan[0].id
    // })
  },
  handleTap() {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: res => {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        this.setData({
          imageSrc: tempFilePaths[0],
          tuisshow: true
        })
      }
    })
  },
  cropperDone(e) {
    var that = this
    console.log(e)
    const {
      src,
      cropperData
    } = e.detail;
    console.log(src)
    wx.uploadFile({
      url: yuming + 'Besall/Upload',
      filePath: src,
      name: 'file',
      formData: {

      },
      success(res) {
        var data = JSON.parse(res.data)
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        console.log(data)
        var urls = data.imgUrl
        // var url =  data.imgUrl
        wx.request({
          url: yuming + 'shop/shopSaveBossBg',
          method: 'POST',
          header: {
            'Content-Type': 'application/x-www-form-urlencoded',
            token: wx.getStorageSync('token'),
            source: 'EabEnd'
          },
          data: {
            shopId: wx.getStorageSync('shopId'),
            bossBackg: urls
          },
          success: function (res) {
            console.log(res)
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
            setTimeout(function () {
              that.liebiao()
              that.setData({
                tuisshow: false
              })
            }, 1000)
          },
          fail: function () {
            app.consoleLog("请求数据失败");
          }
        })
      }
    })

    // wx.previewImage({
    //   current: '', // 当前显示图片的http链接
    //   urls: [src] // 需要预览的图片http链接列表
    // })
  },
  cropperCancel() {
    this.setData({
      tuisshow: false
    })
    console.log('cancel');
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.cropper = this.selectComponent("#yeyouzi-cropper");

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.liebiao()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})